<template>
  <div class="search-result-container">
    <!-- 点击实现后退效果 -->
    <van-nav-bar
      title="搜索结果"
      left-arrow
      @click-left="$router.go(-1)"
      fixed
    />
    <!-- 搜索结果列表 -->
    <van-list
  v-model="loading"
  :finished="finished"
  finished-text="没有更多了"
  @load="onLoad"
  offset="50"
  :immediate-check='false'
>
  <ArticleItem v-for="obj in results" :key="obj.art_id" :item='obj' :isShown='false'></ArticleItem>
    </van-list>
  </div>
</template>

<script>
import ArticleItem from '@/components/ArticleItem.vue'
import { searchResultAPI } from '@/api'
export default {
  components: {
    ArticleItem
  },
  created () {
    this.getResultsList()
  },
  data () {
    return {
      results: [], // 搜索结果数据
      page: 1, // 页码
      loading: false, // 控制加载状态
      finished: false// 是否加载全部数据
    }
  },
  methods: {
    onLoad () {
      this.page++
      this.getResultsList()
    },
    // 获取搜索结果数据
    async  getResultsList () {
      const res = await searchResultAPI({ q: this.$route.params.keywords, page: this.page })
      if (res.data.data.results.length === 0) {
        this.finished = true
        return
      }
      this.results = [...this.results, ...res.data.data.results]
      this.loading = false
    }
  },
  name: 'SearchResult'
}
</script>

<style lang="less" scoped>
.search-result-container {
  padding-top: 46px;
}
</style>
